package ServletController;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import classeMetier.Personn;
public class ServletController extends javax.servlet.http.HttpServlet
    implements javax.servlet.Servlet {
  private static final long serialVersionUID = 1L;


  public ServletController() {
    super();
    }
  public void init(ServletConfig config) throws ServletException {
	    System.out.println("*** initializing controller servlet.");
	    super.init(config);

	    Personn p = new Personn();
	    p.setMatricule(Integer.parseInt(config.getInitParameter("matricule")));
	    p.setName(config.getInitParameter("name"));
	    p.setPrenom(config.getInitParameter("prenom"));
	    p.setAge(config.getInitParameter("age"));
	    ServletContext context = config.getServletContext();
	    context.setAttribute("person", p);
	    }

	    

	    
	   

	  protected void doGet(HttpServletRequest request,
	      HttpServletResponse response) throws ServletException, IOException {
	    doPost(request, response);
	    }


  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    String base = "/WEB-INF/";
    String url = base + "test.jsp";
    String action = request.getParameter("action");
    if (action != null) {
      if (action.equals("test1"))
        url = base + "test1.jsp";
    }
      RequestDispatcher requestDispatcher =
      getServletContext().getRequestDispatcher(url);
    requestDispatcher.forward(request, response);
    }
  }
